import React, { useEffect, FC, useRef } from 'react'

import { Table, Tag, Space, Button, Popconfirm } from 'antd'

import { ListModelState, ConnectProps, Loading, connect, Dispatch } from 'umi'

interface PageProps extends ConnectProps {
  abc: ListModelState
  dispatch: Dispatch
}

const index: FC<PageProps> = (props) => {

  const confirm = key => {
    return () => {
      props.dispatch({
        type: 'abc/remove',
        key
      })
    }
  }
  
  const cancel = () => {
    console.log(200)
  }

  const columns = useRef([
    {
      title: 'Name',
      dataIndex: 'name',
      key: 'name',
      render: (text: string) => <a>{text}</a>,
    },
    {
      title: 'Age',
      dataIndex: 'age',
      key: 'age',
    },
    {
      title: 'Address',
      dataIndex: 'address',
      key: 'address',
    },
    {
      title: 'Tags',
      key: 'tags',
      dataIndex: 'tags',
      render: (tags: Array<any>) => (
        <>
          {tags.map(tag => {
            let color = tag.length > 5 ? 'geekblue' : 'green';
            if (tag === 'loser') {
              color = 'volcano';
            }
            return (
              <Tag color={color} key={tag}>
                {tag.toUpperCase()}
              </Tag>
            );
          })}
        </>
      ),
    },
    {
      title: 'Action',
      key: 'action',
      render: (text, record) => (
        <Space size="middle">
          <a>Invite {record.name}</a>
          <Popconfirm
            title="Are you sure to delete this task?"
            onConfirm={confirm(record.key)}
            onCancel={cancel}
            okText="Yes"
            cancelText="No"
          >
            <Button>Delete</Button>
          </Popconfirm>
        </Space>
      ),
    },
  ])

  useEffect(() => {
    props.dispatch({
      type: 'abc/loaddataEffect',
      uri: '/api/table/list'
    })
  }, [])

  return (
    <div>
      <Table columns={columns.current} dataSource={props.abc.list} />
    </div>
  )
}

export default connect(
  ({abc} : { abc: ListModelState}) => {
    return {
      abc
    }
  }
)(index)